package org.jeecg.modules.com.pointoperation.controller;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.modules.com.pointoperation.entity.Userspoint;
import org.jeecg.modules.com.pointoperation.service.IUserspointService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: userspoint
 * @Author: jeecg-boot
 * @Date:   2020-05-05
 * @Version: V1.0
 */
@Api(tags="userspoint")
@RestController
@RequestMapping("/org.jeecg.com.pointoperation/userspoint")
@Slf4j
public class UserspointController extends JeecgController<Userspoint, IUserspointService> {
	@Autowired
	private IUserspointService userspointService;

	/**
	 * 分页列表查询
	 *
	 * @param userspoint
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "userspoint-分页列表查询")
	@ApiOperation(value="userspoint-分页列表查询", notes="userspoint-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Userspoint userspoint,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {


			Map<String, String[]> parameter = new LinkedHashMap<>();


		req.getParameterMap().keySet().forEach(k->{
			if (k.equals("column")) {
				String[] column = new String[1];
				column[0] = new String("createtime");
				parameter.put(k, column);

			}else {
				String[] value = req.getParameterMap().get(k);
				parameter.put(k, value);
			}
		});


		QueryWrapper<Userspoint> queryWrapper = QueryGenerator.initQueryWrapper(userspoint, parameter);
		Page<Userspoint> page = new Page<Userspoint>(pageNo, pageSize);
//		IPage<Userspoint> pageList = userspointService.page(page, queryWrapper);
		IPage<Userspoint> pageList = userspointService.pageChooseDS(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 *   添加
	 *
	 * @param userspoint
	 * @return
	 */
	@AutoLog(value = "userspoint-添加")
	@ApiOperation(value="userspoint-添加", notes="userspoint-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Userspoint userspoint) {
		userspointService.save(userspoint);
		return Result.ok("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param userspoint
	 * @return
	 */
	@AutoLog(value = "userspoint-编辑")
	@ApiOperation(value="userspoint-编辑", notes="userspoint-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Userspoint userspoint) {
		userspointService.updateById(userspoint);
		return Result.ok("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "userspoint-通过id删除")
	@ApiOperation(value="userspoint-通过id删除", notes="userspoint-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		userspointService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "userspoint-批量删除")
	@ApiOperation(value="userspoint-批量删除", notes="userspoint-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.userspointService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "userspoint-通过id查询")
	@ApiOperation(value="userspoint-通过id查询", notes="userspoint-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Userspoint userspoint = userspointService.getById(id);
		if(userspoint==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(userspoint);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param userspoint
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Userspoint userspoint) {
        return super.exportXls(request, userspoint, Userspoint.class, "userspoint");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Userspoint.class);
    }

    @GetMapping("listall")
	 public Result<?> testProc(HttpServletRequest request) {

		List<Userspoint> result = userspointService.listAllUserspoint();

		return Result.ok(result);

	}

}
